import $ from 'jquery';

import { function_handlelogout } from '../actions/passport'; 

//商品名称
export const PRODUCT_NAME = "流刀上线工具";

export const AJAX_BASE_URL = process.env.NODE_ENV === "development" ? "http://127.0.0.1:8080/" : "http://api.companyclub.cn/";
export const H5_URL = process.env.NODE_ENV === "development" ? "http://127.0.0.1:3000" : "http://h5.companyclub.cn";

//客户端项目地址
export const GITEE_URL = "https://gitee.com/onlinetool/liudao";
export const GITEE_DOWNLOAD_URL = GITEE_URL + "/releases";

//url地址列表
export const URL = {
  "register_page": "/register",
  "findpwd_page": "/findpwd",
  "login_page": "/login",
  "download_page": "/download",
  "main_page": "/bash",
  "team_setting_page": "/bash/team",
  "project_page": "/project",
  "project_setting_page": "/project/setting",
  "members_page": "/members/:pid",
  "serv_page": "/serv",
  "addserv_page": "/serv/add",
  "editserv_page": "/serv/edit",
  "hosts_page": "/bash/hosts",
  "databases_page": "/bash/databases",
  "deploying_page": "/bash/deploying",
  "lined_page": "/bash/deployed",
  "addtask_page": "/project/mergerequest",
  "trashtask_page": "/project/trashcan",
  "closetask_page": "/project/closetask",
  "taskdetail_page": "/bash/task",
};

export const API_URL = {
  "register": "register", //注册
  "login": "login", //登录
  "getVerifyImage": "verifyImg", //验证码图片
  "sendVerifyEmail": "sendVerifyCode", //发送找回验证码的邮件
  "chgPwdByEmail": "changePwdByEmail", //使用邮箱更改密码
  "chgPwdByUid": "setPassword", //修改密码
  "setMineNick": "updateNickname", //修改昵称
  "setTeamName": "setTeamName", //设置团队名称
  "getUserInfo": "getUserinfo", //获取用户信息
  "setMineAvatar": "uploadAvatar", //修改头像
  "addMemberByProject": "addMember", //项目添加成员
  "addProject": "addProject", //添加项目
  "getProject": "getProject", //获取项目详细信息
  "setProjectName": "setProjectName", //修改项目名称
  "setProjectIcon": "setProjectIcon", //设置项目图标
  "rmRole": "removeMemberByRole", //删除某个角色
  "setProjectNotify": "setProjectChathook", //设置项目聊天钩子
  "setClientUrl": "setDeployUrl", //设置客户端所在地址
  "addMergeRequest": "addTask", //提交上线请求
  "saveMergeRequest": "saveTask", //保存上线单草稿
  "addMergeRequestFromTrash": "addTaskFromTrash", //提交上线请求
  "saveMergeRequestFromTrash": "saveTaskFromTrash", //保存上线单草稿
  "getTask": "getTask", //获取任务详情
  "deployingTask": "getDeployTask", //待上线的请求
  "getTrashTask": "getSavedTask", //上线请求草稿
  "getLineTask": "getDeployedTask", //已上線的任務
  "ckVersion": "ckversion", //检测版本更新
  "lsProjectHost": "lsHost", //列出主机列表
  "lsProjectDb": "lsDbConnector", //列出数据库连接列表
  "addProjectHost": "addHost", //添加服务器
  "removeProjectDbConnector": "rmDbConnector", //删除数据库链接
  "removeProjectHost": "rmHost", //删除服务器
  "addProjectDbConnector": "addDbConnector", //添加数据库连接
  "addServersByProject": "addServs",//添加微服务 
  "editServersById": "editServs",//编辑微服务
  "getServerById": "getServs", //获取微服务信息
  "lsBranchesByDir": "ckgit", //获取指定目录的分支列表
  "lsBranchesByServsId": "getBranchesList", //获取指定微服务的分支列表
  "listServers": "lsServs", //列出项目全部微服务列表
  "getDeployLogger": "getLogger", //获取指定微服务的日志
  "setTaskTester": "chgTaskTester", //修改任务的测试人员
  "setTaskAdmin": "chgTaskReviewer", //修改任务的上线人员
  "setTaskDeploying": "setDeploying", //设置任务为部署中的状态
  "setTaskFailured": "markDeployFailured", //设置任务上线失败
  "closeTask": "setClosed", //关闭上线申请
  "setTaskTesting": "setTesting", //设置任务为测试环境测试中状态
  "setTaskPreTesting": "setPreTesting", //设置任务为预发布环境测试中状态
  "closeTest": "abortTest", //中止测试
  "closePreTest": "abortPreTest", //中止预发布环境测试
  "setTaskTestSuccess": "setTestSuccess", //设置任务测试通过
  "setTaskPrevTestSuccess": "setPreTestSuccess", //设置任务预发布环境测试通过
  "deployTaskOnline": "deployTaskOnline", //部署项目
  "deployTaskTest": "deployTaskTest", //部署到测试环境
  "deployTaskPre": "deployTaskPre", //部署到预发布环境
  "diffFileByService": "diffFileByService", //按照微服务比较文件
}

//错误码
export const CODE = {
  SUCCESS: 200,
  PARAM_ERROR: 1002,
  FORBIDDEN: 401,
};

//任务流转状态码
export const TaskStatus = {
  /**
   * 等待提交上线任务状态
   */
  WAIT_FOR_SUBMIT: 1,
  /**
   * 上线任务等待测试人员测试状态
   */
  WAIT_FOR_TEST: 2,
  /**
   * 测试中状态
   */
  TESTING: 3,
  /**
   * 测试不通过状态
   */
  TEST_FAILED: 4,
  /**
   * 等待上线状态
   */
  WAIT_FOR_DEPLOY: 5,
  /**
   * 预发布环境测试中状态
   */
  PRE_TESTING: 6,
  /**
   * 预发布环境测试失败状态
   */
  PRE_TESTING_FAIL: 7,
  /**
   * 预发布环境测试通过状态
   */
  PRE_TESTING_SUCCESS: 8,
  /**
   * 上线被拒绝状态
   */
  DEPLOY_FAILED: 9,
  /**
   * 上线成功状态
   */
  DEPLOY_SUCCESS: 10,
  /**
   * 上线中
   */
  DEPLOYING: 12,
}

//任务详情页来源
export const TASKDETAIL_FROM = {
  DEPLOYING: 1, //待上线分支列表
  ADDTASK: 2 //添加任务后的跳转
};

export function upFileAjax (url, data, cb) {
  let formData = new FormData();

  let uid = parseInt(window.localStorage.getItem("_uid"));
  let organizeid = parseInt(window.localStorage.getItem("_organizeid"));
  let timestamp = parseInt(window.localStorage.getItem("_timestamp"));
  let token = window.localStorage.getItem("_token");
  if (uid == null || organizeid == null || timestamp == null || token == null) {
    window.location.href = URL.login_page;
    return;
  }
  formData.append("uid", uid);
  formData.append("organizeid", organizeid);
  formData.append("timestamp", timestamp);
  formData.append("token", token);
  Object.keys(data).forEach(function (key) {
    formData.append(key, data[key]);
  });
  $.ajax({
    url: AJAX_BASE_URL + url,
    type: "post",
    dataType: 'json',
    async: false,
    data: formData,
    processData: false,
    contentType: false,
    success: function (result) {
      if (result.code === CODE.FORBIDDEN) {
        window.location.href = URL.login_page;
        return;
      }
      cb(result);
    },
    error: function (response) {
      alert("网络连接错误");
    }
  });
}

export function MyAjax (url, data, cb) {
  if ($.inArray(url, [
    API_URL.register,
    API_URL.login,
    API_URL.sendVerifyEmail,
    API_URL.chgPwdByEmail
  ]) === -1) {
    let uid = parseInt(window.localStorage.getItem("_uid"));
    let organizeid = parseInt(window.localStorage.getItem("_organizeid"));
    let timestamp = parseInt(window.localStorage.getItem("_timestamp"));
    let token = window.localStorage.getItem("_token");
    if (uid == null || organizeid == null || timestamp == null || token == null) {
      function_handlelogout();
      window.location.href = URL.login_page;
      return;
    }
    data = Object.assign({}, data, {
      uid: uid,
      organizeid: organizeid,
      timestamp: timestamp,
      token: token
    });
  }
  $.ajax({
    url: AJAX_BASE_URL + url,
    type: "post",
    timeout: 6000,
    traditional: true,
    data,
    dataType: "json",
    xhrFields: {
      withCredentials: true
    },
    success: function (result) {
      if (result.code === CODE.FORBIDDEN) {
        function_handlelogout();
        window.location.href = URL.login_page;
        return;
      }
      cb(result);
    },
    error: function () {
      alert("网络连接错误");
    }
  });
}

export function ClientAjax (deployUrl, url, data, cb) {
  let uid = parseInt(window.localStorage.getItem("_uid"));
  let organizeid = parseInt(window.localStorage.getItem("_organizeid"));
  let timestamp = parseInt(window.localStorage.getItem("_timestamp"));
  let token = window.localStorage.getItem("_token");
  data = Object.assign({}, data, {
    uid: uid,
    organizeid: organizeid,
    timestamp: timestamp,
    token: token
  });
  $.ajax({
    url: deployUrl + url,
    type: "post",
    timeout: 12000,
    data,
    dataType: "json",
    success: function (result) {
      cb(result);
    }
  });
}